In [3]:
%load_ext sql
%sql sqlite:///chinook.db


The sql extension is already loaded. To reload it, use:
  %reload_ext sql
Out[3]:
'Connected: None@chinook.db'

Задание 1

Вывести 10 самых больших по размеру треков жанра ROCK и формата MPEG


In [4]:
%%sql
SELECT t.name
FROM tracks t 
    INNER JOIN genres g 
    ON t.genreid = g.genreid
    INNER JOIN media_types m 
    ON m.mediatypeid = t.mediatypeid
ORDER BY t.bytes desc 
limit 10


Done.
Out[4]:
Name
Through a Looking Glass
Occupation / Precipice
The Young Lords
The Man With Nine Lives
Dave
The Magnificent Warriors
The Lost Warrior
Maternity Leave
Battlestar Galactica, Pt. 3
The Woman King

Задание 2

Вывести названия всех групп, их песен и названия их альбомов для всех треков жанра Рок, приобретенные сотрудниками Microsoft.


In [51]:
%%sql
SELECT distinct ar.name, t.name, a.title
FROM tracks t
    INNER JOIN albums a
    ON a.albumid = t.albumid
    INNER JOIN artists ar
    ON a.artistid = ar.artistid
    INNER JOIN invoice_items i
    ON i.trackid = t.trackid
    INNER JOIN invoices ii 
    on ii.invoiceid = i.invoiceid
    INNER JOIN customers c
    ON ii.customerid = c.customerid
    INNER JOIN genres g
    ON t.genreid = t.genreid
WHERE c.company like '%Microsoft%'
AND g.name = 'Rock'


Done.
Out[51]:
Name Name_1 Title
Spyro Gyra Believe Heart of the Night
Spyro Gyra As We Sleep Heart of the Night
Guns N' Roses Double Talkin' Jive Use Your Illusion I
Guns N' Roses The Garden Use Your Illusion I
Guns N' Roses Don't Damn Me Use Your Illusion I
Guns N' Roses Dead Horse Use Your Illusion I
Metallica Attitude ReLoad
Metallica For Whom The Bell Tolls Ride The Lightning
Metallica Creeping Death Ride The Lightning
Metallica Some Kind Of Monster St. Anger
Metallica Shoot Me Again St. Anger
Metallica All Within My Hands St. Anger
Caetano Veloso Meditação Prenda Minha
Toquinho & Vinícius Formosa Vinícius De Moraes - Sem Limite
Toquinho & Vinícius Minha Namorada Vinícius De Moraes - Sem Limite
Falamansa Zeca Violeiro Deixa Entrar
Foo Fighters No Way Back In Your Honor [Disc 1]
Foo Fighters Still In Your Honor [Disc 2]
Foo Fighters Razor In Your Honor [Disc 2]
Foo Fighters Overdrive One By One
Foo Fighters My Hero The Colour And The Shape
Frank Sinatra New York, New York My Way: The Best Of Frank Sinatra [Disc 1]
Frank Sinatra My Kind Of Town My Way: The Best Of Frank Sinatra [Disc 1]
Frank Sinatra Bad, Bad Leroy Brown My Way: The Best Of Frank Sinatra [Disc 1]
Funk Como Le Gusta Zambação Roda De Funk
Funk Como Le Gusta Divirta-Se (Saindo Da Sua) Roda De Funk
Gilberto Gil Assum Preto As Canções de Eu Tu Eles
Gilberto Gil Is This Love (Live) Quanta Gente Veio Ver (Live)
Gilberto Gil Copacabana (Live) Quanta Gente Veio Ver (Live)
Tim Maia Formigueiro Serie Sem Limite (Disc 2)
Titãs O Pulso Acústico
Titãs A Melhor Forma Acústico
Titãs Homem Primata Acústico
Titãs Lugar Nenhum Volume Dois
Titãs Caras Como Eu Volume Dois
Titãs Toda Cor Volume Dois
Battlestar Galactica Exodus, Pt. 2 Battlestar Galactica, Season 3
Battlestar Galactica The Passage Battlestar Galactica, Season 3

Задание 3

Для каждого набора (жанр, тип медиа) вывести среднюю цену по стоимости трека и общее количество, причем вывести только те наборы, для которых все треку стоят больше 1,5$.


In [12]:
%%sql
WITH A 
AS 
(
select g.genreid, g.name, m.mediatypeid, m.name as m_name, i.unitprice
from tracks t 
inner join media_types m
  on t.mediatypeid = m.mediatypeid
inner join genres g 
  on t.genreid = t.genreid
inner join invoice_items i
  on i.trackid = t.trackid
    
)

select name, m_name, avg(unitprice) as avg_unitprice
from A
where not exists
(
select * 
from A Inn
where A.genreid = Inn.genreid
and A.mediatypeid = Inn.mediatypeid 
and unitprice <= 1.5)
group by genreid, mediatypeid, name, m_name


Done.
Out[12]:
name m_name avg_unitprice
Rock Protected MPEG-4 video file 1.9900000000000024
Jazz Protected MPEG-4 video file 1.9900000000000024
Metal Protected MPEG-4 video file 1.9900000000000024
Alternative & Punk Protected MPEG-4 video file 1.9900000000000024
Rock And Roll Protected MPEG-4 video file 1.9900000000000024
Blues Protected MPEG-4 video file 1.9900000000000024
Latin Protected MPEG-4 video file 1.9900000000000024
Reggae Protected MPEG-4 video file 1.9900000000000024
Pop Protected MPEG-4 video file 1.9900000000000024
Soundtrack Protected MPEG-4 video file 1.9900000000000024
Bossa Nova Protected MPEG-4 video file 1.9900000000000024
Easy Listening Protected MPEG-4 video file 1.9900000000000024
Heavy Metal Protected MPEG-4 video file 1.9900000000000024
R&B/Soul Protected MPEG-4 video file 1.9900000000000024
Electronica/Dance Protected MPEG-4 video file 1.9900000000000024
World Protected MPEG-4 video file 1.9900000000000024
Hip Hop/Rap Protected MPEG-4 video file 1.9900000000000024
Science Fiction Protected MPEG-4 video file 1.9900000000000024
TV Shows Protected MPEG-4 video file 1.9900000000000024
Sci Fi & Fantasy Protected MPEG-4 video file 1.9900000000000024
Drama Protected MPEG-4 video file 1.9900000000000024
Comedy Protected MPEG-4 video file 1.9900000000000024
Alternative Protected MPEG-4 video file 1.9900000000000024
Classical Protected MPEG-4 video file 1.9900000000000024
Opera Protected MPEG-4 video file 1.9900000000000024

Задание 4

Вывести компании, сделавшие максимальное и минимальное число заказов.


In [17]:
%%sql
WITH A 
AS 
(
select c.company, count(i.invoiceid) as cnt
from customers c 
  inner join invoices i
    on i.customerid = c.customerid
where company is not null
group by company
)
SELECT * 
FROM A
where cnt in (select min(cnt) from A union select max(cnt) from A )


Done.
Out[17]:
company cnt
Apple Inc. 7
Banco do Brasil S.A. 7
Embraer - Empresa Brasileira de Aeronáutica S.A. 7
Google Inc. 7
JetBrains s.r.o. 7
Microsoft Corporation 7
Riotur 7
Rogers Canada 7
Telus 7
Woodstock Discos 7

Задание 5

Для каждой компании вывести общее количестов песен, купленных по жанру поп-музыки


In [21]:
%%sql 
select c.company, count(*) as cnt
from tracks t 
inner join genres g
  on t.genreid = g.genreid
inner join invoice_items ii 
  on t.trackid = ii.trackid
inner join invoices i 
  on i.invoiceid = ii.invoiceid
inner join customers c 
  on c.customerid = i.customerid 
where g.name = 'Pop'
and c.company is not null
group by c.company


Done.
Out[21]:
Company cnt
Embraer - Empresa Brasileira de Aeronáutica S.A. 2
JetBrains s.r.o. 4

Задание 6

Вывести средний размер альбома в байтах.


In [29]:
%%sql

Select avg(sb) as avg_sb
from 
(
select al.title, sum(bytes) as sb 
from albums al
inner join tracks t 
  on al.albumid = t.albumid
group by al.title
)


Done.
Out[29]:
avg_sb
338288920.3170029

In [ ]: